Imports System.IO
Imports System.Windows.Forms

Public Class frmUPDATE_VERSION
    Dim ExeFileName As String = "www.TOTAL-POS.net.exe"
    Dim Host As String = "\\frank\B1_SHR"
    Private _totalFileSize As Long = 0
    Private _totalBytesCopied As Long = 0
    Private _copyProgressRoutine As CopyProgressRoutine
    Private Delegate Function CopyProgressRoutine(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
    Private Declare Auto Function CopyFileEx Lib "kernel32.dll" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal lpProgressRoutine As CopyProgressRoutine, ByVal lpData As Int32, ByVal lpBool As Int32, ByVal dwCopyFlags As Int32) As Int32

    Private Function CopyProgress(ByVal totalFileSize As Int64, ByVal totalBytesTransferred As Int64, ByVal streamSize As Int64, ByVal streamBytesTransferred As Int64, ByVal dwStreamNumber As Int32, ByVal dwCallbackReason As Int32, ByVal hSourceFile As Int32, ByVal hDestinationFile As Int32, ByVal lpData As Int32) As Int32
        UiProgressBar1.Value = Convert.ToInt32((_totalBytesCopied + totalBytesTransferred) / _totalFileSize * 100)
        Application.DoEvents()
    End Function

    Private Sub GetTotalFileSize(ByVal folder As System.IO.DirectoryInfo)
        For Each fi As System.IO.FileInfo In folder.GetFiles
            _totalFileSize += fi.Length
        Next
        'For Each di As System.IO.DirectoryInfo In folder.GetDirectories
        '    GetTotalFileSize(di)
        'Next
    End Sub

    Private Sub CopyFiles(ByVal folder As System.IO.DirectoryInfo, ByVal destinationFolder As String)
        If Not System.IO.Directory.Exists(destinationFolder) Then
            System.IO.Directory.CreateDirectory(destinationFolder)
        End If
        For Each fi As System.IO.FileInfo In folder.GetFiles
            File.Copy(fi.FullName, destinationFolder & "\" & fi.Name, True)
            'CopyFileEx(fi.FullName, destinationFolder & "\" & fi.Name, _copyProgressRoutine, 0, 0, 0)
            _totalBytesCopied += fi.Length
        Next
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim LocalVersion As String
            Dim ServerVersion As String
            Dim versionInfo As FileVersionInfo
            versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(Application.StartupPath + "\" + ExeFileName)
            LocalVersion = versionInfo.FileVersion()
            versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(Host + "\" + ExeFileName)
            ServerVersion = versionInfo.FileVersion

            Dim mWSFileServer As New localhost.TOTALPOS_WS
            Dim mFileInformation() As localhost.FileInformation

            mFileInformation = mWSFileServer.Browse("", localhost.FileExtensions.all)
            For i As Integer = 0 To mFileInformation.Length - 1
                With mFileInformation(i)
                    If Not SaveFile(.Name, .FileContent) Then
                        MessageBox.Show("ERROR")
                    End If
                End With
            Next

            Return
            If CInt(LocalVersion.Replace(".", "")) < CInt(ServerVersion.Replace(".", "")) Then
                KillAppExe()
                DownloadFile()
                Dim fileApp As New FileInfo(ExeFileName)
                If (fileApp.Exists) Then
                    Process.Start(ExeFileName)
                End If
            End If
            
            Application.Exit()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            Me.Close()
            Application.Exit()
        End Try
    End Sub

    Private Sub DownloadFile()
        _totalFileSize = 0
        _totalBytesCopied = 0
        lb_Info.Text = "Downloading files on server..."
        GetTotalFileSize(New System.IO.DirectoryInfo(Host))
        _copyProgressRoutine = New CopyProgressRoutine(AddressOf CopyProgress)
        CopyFiles(New System.IO.DirectoryInfo(Host), Application.StartupPath)
    End Sub
    Sub KillAppExe()
        ' Get MainApp exe name without extension
        Dim AppExe As String = ExeFileName.Replace(".exe", "")

        Dim local As Process() = Process.GetProcesses
        Dim i As Integer
        ' Search MainApp process in windows process
        For i = 0 To local.Length - 1
            ' If MainApp process found then close or kill MainApp
            If Strings.UCase(local(i).ProcessName) = Strings.UCase(AppExe) Then
                local(i).CloseMainWindow()
            End If
        Next
    End Sub
    Public Function SaveFile(ByVal Name As String, _
                           ByVal Content As Byte()) As Boolean
        Dim objFstream As FileStream
        Try
            objFstream = File.Open(Application.StartupPath & "\" & Name, _
                            FileMode.Create, FileAccess.Write)
            Dim lngLen As Long = Content.Length
            objFstream.Write(Content, 0, CInt(lngLen))
            objFstream.Flush()
            Return True
        Catch exp As Exception
            Return False
        Finally
            objFstream.Close()
        End Try
    End Function
End Class
